package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import com.zhongwang.mms.module.half.model.HOrderDetailExport;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 出入库查询
 */
@Log4j2
@Service
public class InAndOutService {
    @Autowired
    private HOrderDetailDao hOrderDetailDao;

    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;

    public List<HOrderDetailModel> findAll(HOrderDetailModel hOrderDetailModel) {
        return hOrderDetailDao.findAllByQuery(hOrderDetailModel);
    }

    /**
     * 查询成品入库
     *
     * @param hOrderDetailModel
     * @return
     */
    public List<HOrderDetailModel> getInFinish(HOrderDetailModel hOrderDetailModel) {
        return hOrderDetailDao.selectDetailByOdrId(hOrderDetailModel.getOdrId());
    }

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 导出
     *
     * @param param
     */
    public JsonResult exportExcel(HOrderDetailModel param) {
        JsonResult jsonResult = new JsonResult();
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("成品库出入库综合查询——导出","用户:【"+user.getName()+"】执行导出操作！");
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        String excelFileName = "成品库出入库综合查询.xlsx";
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            Subject userSubject = ShiroUtils.getSubject();
            //2.查询出数据导入到临时表
            if (userSubject.isPermitted("halwar:amount:view")) {
                hOrderDetailDao.createAllExportByQueryTempTable(param);
            } else {
                hOrderDetailDao.createAllExportByQueryTempTableNotMoney(param);
            }
            //3.导出Excel
            if (ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active,true)) {
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setData(excelFileName);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    /**
     * 调拨入库明细
     *
     * @param param
     * @return
     */
    public List<HOrderDetailModel> selectAllocationDetailList(HOrderDetailModel param) {
        return hOrderDetailDao.findAllByOdrId(param.getOdrId());
    }
}
